#ifndef __BSP_SDRAM_H_
#define __BSP_SDRAM_H_
#include "gd32f4xx.h"

/**********************************************************************
* SDRAM在单片机外设中有对应的固定地址 W9825G6KH-6I(166MHz/CL3 or 133MHz/CL2)
* GD32F4xx EXMC_NCAS引脚:列地址选通，低电平有效
* GD32F4xx EXMC_NRAS引脚:行地址选通，低电平有效
* GD32F4xx EXMC_SDNE[1:0]引脚: SDRAM device(0/1)的片选使能信号，低电平有效
* GD32F4xx EXMC_SDCKE[1:0]引脚: SDRAM device(0/1)的时钟使能信号
* GD32F4xx EXMC_SDNWE引脚:写使能，低电平有效
* GD32F4xx EXMC_SDCLK引脚: 同步时钟线
* GD32F4xx EXMC_D[31:0]引脚:读/写数据
* GD32F4xx EXMC_A[25:0]引脚:Bank 地址
* GD32F4xx EXMC_NBL[1:0]引脚:写数据标记（掩码）
***********************************************************************
* EXMC_D0 -> PD14 //数据线
* EXMC_D1 -> PD15
* EXMC_D2 -> PD0
* EXMC_D3 -> PD1
* EXMC_D4 -> PE7
* EXMC_D5 -> PE8
* EXMC_D6 -> PE9
* EXMC_D7 -> PE10
* EXMC_D8 -> PE11
* EXMC_D9 -> PE12
* EXMC_D10 -> PE13
* EXMC_D11 -> PE14
* EXMC_D12 -> PE15
* EXMC_D13 -> PD8
* EXMC_D14 -> PD9
* EXMC_D15 -> PD10
* 
* EXMC_A0 -> PF0 //地址线
* EXMC_A1 -> PF1
* EXMC_A2 -> PF2
* EXMC_A3 -> PF3
* EXMC_A4 -> PF4
* EXMC_A5 -> PF5
* EXMC_A6 -> PF12
* EXMC_A7 -> PF13
* EXMC_A8 -> PF14
* EXMC_A9 -> PF15
* EXMC_A10 -> PG0
* EXMC_A11 -> PG1
* EXMC_A12 -> PG2
* 
* EXMC_NBL1 -> PE1
* EXMC_NBL0 -> PE0
* 
* EXMC_SDCLK -> PG8
* 
* EXMC_SDNWE -> PC0
* 
* EXMC_SDCKE -> PC3
* 
* EXMC_SDNE -> PC2
* 
* EXMC_NRAS -> PF11
* 
* EXMC_NCAS -> PG15
* 
* SDRAM_BS0 -> PG4 
* 
* SDRAM_BS1 -> PG5
*********************************************************************/
#define SDRAM_MODEREG_BURST_LENGTH_1             ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_LENGTH_2             ((uint16_t)0x0001)
#define SDRAM_MODEREG_BURST_LENGTH_4             ((uint16_t)0x0002)
#define SDRAM_MODEREG_BURST_LENGTH_8             ((uint16_t)0x0003)

#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL      ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED     ((uint16_t)0x0008)

#define SDRAM_MODEREG_CAS_LATENCY_2              ((uint16_t)0x0020)
#define SDRAM_MODEREG_CAS_LATENCY_3              ((uint16_t)0x0030)

#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE     ((uint16_t)0x0200)

#define SDRAM_MODEREG_OPERATING_MODE_STANDARD    ((uint16_t)0x0000)

#define SDRAM_START_ADDR ((uint32_t)(0XC0000000))



void sdram_write_buf(uint8_t *buf, uint32_t addr, uint32_t len);
void sdram_read_buf(uint8_t *buf, uint32_t addr, uint32_t len);
uint8_t bsp_sdram_config(void);
void sdram_write_read_test(void);
#endif